<?php


namespace app\admin\controller\Free;


use app\admin\controller\AppController;
use app\admin\model\Free\FreeOrderModel;
use app\admin\model\System\LogisticsModel;
use app\Request;
use app\Utils\ImageUtil;
use app\Utils\Util;

class FreeOrder extends AppController
{
    /**
     * 分页查询
     * @param Request $request
     * @return array
     */
    public function getFreeOrderList(Request $request)
    {
        $param = $request->param();
        $where = [];
        $page = !empty($param['page']) ? $param['page'] : 1;
        $limit = !empty($param['limit']) ? $param['limit'] :10;
        if (!empty($param['keyword'])) {
            array_push($where, ['order_sn', 'like', '%' . $param['keyword'] . '%']);
        }
        if (isset($param['status'])) {
            array_push($where, ['status', '=', $param['status']]);
        }
        $list = FreeOrderModel::getFreeOrderListWhere($page, $limit, $where);
        $count = FreeOrderModel::getFreeOrderCountWhere($where);
        foreach ($list as &$item){
            $goods_info = json_decode($item['goods_info'],true);
            $item['list'][0]['info'] = [
                'name'           => $goods_info['good_name'],
                'img'            => ImageUtil::i()->replaceUrl($goods_info['img']),
                'number'         => $item['pay_num'],
                'slider_img'     => $goods_info['slider_img'],
                'price'          => $item['price'],
                'total_price'    => $item['total_price']
            ];
            $item['list'][0]['add_time'] = $item['add_time'];
            $item['list'][0]['lgoistics_name'] = $item['lgoistics_name'];
            $item['list'][0]['lgoistics_num'] = $item['lgoistics_num'];
        }
        return Util::returnPageJson(self::CODE_DATA, $count, $list, "");
    }


    /**
     * 发货
     * @param Request $request
     */
    public function deliverFreeOrder(Request $request)
    {
        $param = $request->param();
        $logistic = LogisticsModel::getLogisticsWhere([['com','=',$param['lgoistics_com'][0]]]);
        $order_save = [
            'status'        => 2,
            'lgoistics_com' => $param['lgoistics_com'][0],
            'lgoistics_num' => $param['lgoistics_num'][0],
            'lgoistics_name'=> $logistic['company_name'],
            'up_time'       => date("Y-m-d H:i:s",time())
        ];
        FreeOrderModel::updateFreeOrder([['id','=',$param['order_id']]],$order_save);
        return Util::returnJson(self::CODE_SUCCESS,null,"发货成功");
    }

}